package com.seckill.aop;

import com.seckill.annotation.LogRecord;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Aspect
@Component
public class LogRecordAOP {
    @Pointcut("@annotation(com.seckill.annotation.LogRecord)")
    public void controllerAspect(){}

    @Around("controllerAspect()")
    public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("执行aop切面");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        LogRecord logRecord = method.getAnnotation(LogRecord.class);
        String value = logRecord.value();
        System.out.println("获取注解得值：" + value);
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args));
        Object obj = joinPoint.proceed();
        return obj;
    }
}
